home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / deskfo.zip / SOURCE.ZIP / DESKFONT.C next >
C/C++ Source or Header  |  1993-09-09  |  11KB  |  327 lines

  1.  
  2. /******************************************************************************
  3. *                                                                                                        *
  4. *            DeskFont Control Panel Applet by Mark Gamber  (Sept 93)            *
  5. *                                                                                                        *
  6. ******************************************************************************/
  7.  
  8. #include "windows.h"
  9. #include "shellapi.h"
  10. #include "commdlg.h"
  11. #include "cpl.h"
  12. #include "ctl3d.h"  //  ..............................Optional, if you have it.
  13. #include "deskfont.h"
  14.  
  15.  
  16. HINSTANCE hInst;
  17. char *TITLE = "DeskFont";
  18. char *HELPFILE = "deskfont.hlp";
  19. BOOL HelpLoaded;
  20.  
  21.  
  22. // ---- DLL Entry Point for Windows NT ----------------------------------------
  23.  
  24. BOOL WINAPI DLLEntryPoint( HINSTANCE hInstance, DWORD dwReason, 
  25.                            LPVOID lpvReserved )
  26. {
  27.     return( TRUE );
  28. }
  29.  
  30. // ---- Control Panel Applet Entry Point --------------------------------------
  31.  
  32. LONG CALLBACK CPlApplet( HWND hWnd, UINT uMsg, LPARAM lParam1, LPARAM lParam2 )
  33. {
  34.    switch( uMsg )
  35.    {
  36.       case CPL_INIT: 
  37.          hInst = GetModuleHandle( "deskfont.cpl" );       //  Find our instance
  38.          return TRUE;
  39.  
  40.       case CPL_GETCOUNT: 
  41.          return( 1 );                                          //  We support one applet
  42.  
  43.       case CPL_NEWINQUIRE:                   //  Tell Control Panel about this applet
  44.         {
  45.             LPNEWCPLINFO lpNewCPlInfo;
  46.  
  47.          lpNewCPlInfo = (LPNEWCPLINFO)lParam2;
  48.  
  49.          lpNewCPlInfo->dwSize = (DWORD)sizeof(NEWCPLINFO);
  50.          lpNewCPlInfo->dwFlags = 0;
  51.          lpNewCPlInfo->dwHelpContext = 0;
  52.          lpNewCPlInfo->lData = 0;
  53.          lpNewCPlInfo->hIcon = LoadIcon( hInst, MAKEINTRESOURCE( 100 ) );
  54.          lpNewCPlInfo->szHelpFile[ 0 ] = '\0';
  55.          LoadString( hInst, 100, lpNewCPlInfo->szName, 32 );
  56.          LoadString( hInst, 101, lpNewCPlInfo->szInfo, 64 );
  57.          break;
  58.         }
  59.  
  60.       case CPL_DBLCLK:                        //  If applet icon is selected...
  61.             Ctl3dRegister( hInst );              //  If you have CTL3D32, register here
  62.             Ctl3dAutoSubclass( hInst );
  63.             HelpLoaded = FALSE;                                //  Initialize help flag
  64.                                                                    //  Start main dialog box
  65.          DialogBox( hInst, MAKEINTRESOURCE( 10000 ), hWnd, DeskfontDlgProc );
  66.             Ctl3dUnregister( hInst );                              //  Kill CTL3D here
  67.          break;
  68.  
  69.         default:
  70.             break;
  71.     }
  72.    return( 0 );
  73. }
  74.  
  75. // ---- Main Applet Dialog Box ------------------------------------------------
  76.  
  77. BOOL WINAPI DeskfontDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  78. {
  79.     switch( msg )
  80.     {
  81.         case WM_INITDIALOG:
  82.         {
  83.             char str[ 64 ];
  84.             char szValue[ 64 ];
  85.             LONG lStatus;
  86.             HKEY hKey;
  87.             DWORD dwSize, dwType;
  88.  
  89.             LoadString( hInst, 200, str, 64 );           //  "Control Panel\\Desktop"
  90.                                                  //  Attempt to open key to path above
  91.             lStatus = RegOpenKeyEx( HKEY_CURRENT_USER, str, (DWORD)0,
  92.                                             KEY_ALL_ACCESS, &hKey );
  93.  
  94.             if( lStatus != ERROR_SUCCESS )        //  If it fails, complain and exit
  95.             {
  96.                 LoadString( hInst, 102, str, 64 );                  //  "Open Key: "
  97.                 ErrorMessageBox( str, TITLE );
  98.                 EndDialog( hDlg, FALSE );
  99.                 return( TRUE );
  100.             }
  101.  
  102.             LoadString( hInst, 201, szValue, 64 );        //  "IconVerticalSpacing"
  103.  
  104.             dwSize = 10;                                //  Get value of V. spacing
  105.             lStatus = RegQueryValueEx( hKey, szValue, (LPDWORD)0, &dwType,
  106.                                                 szValue, &dwSize );
  107.             if( lStatus == ERROR_SUCCESS )           //  If it worked, display value
  108.                 SetDlgItemText( hDlg, 101, szValue );
  109.  
  110.             LoadString( hInst, 202, szValue, 64 );                //  "IconSpacing"                                                                     
  111.                                                              //  Get value of H. spacing
  112.             dwSize = 10;
  113.             lStatus = RegQueryValueEx( hKey, szValue, (LPDWORD)0, &dwType,
  114.                                                 szValue, &dwSize );
  115.             if( lStatus == ERROR_SUCCESS )
  116.                 SetDlgItemText( hDlg, 102, szValue );                //  Display value
  117.  
  118.             RegCloseKey( hKey );                               //  Close key and it's done
  119.             return( TRUE );
  120.         }
  121.  
  122.         case WM_COMMAND:
  123.         {
  124.             if( wParam == IDCANCEL )                      //  Cancel, of course, exits
  125.             {
  126.                 EndDialog( hDlg, FALSE );
  127.                 return( TRUE );
  128.             }
  129.  
  130.             if( wParam == 100 )                        //  Change the desktop font?
  131.             {
  132.                 ChangeDesktopFont( hDlg );                  //  If so, see this proc
  133.                 break;
  134.             }
  135.  
  136.             if( wParam == IDOK )              //  OK saves current values and exits
  137.             {
  138.                 char str[ 64 ];
  139.                 char resname[ 64 ];
  140.                 char pd[ 16 ];
  141.                 HKEY hKey;
  142.                 BOOL bJunk;
  143.                 int val;
  144.                 LONG lStatus;
  145.                 DWORD dwSize;
  146.  
  147.                 LoadString( hInst, 203, pd, 16 );                   //  Load for later
  148.                 LoadString( hInst, 200, str, 64 );      //  "Control Panel\\Desktop"
  149.                                                          //  Open key using path above
  150.                 lStatus = RegOpenKeyEx( HKEY_CURRENT_USER, str, (DWORD)0,
  151.                                                 KEY_ALL_ACCESS, &hKey );
  152.                 if( lStatus != ERROR_SUCCESS )                       //  If it bombed...
  153.                 {
  154.                     LoadString( hInst, 102, str, 64 );               //  "Open Key: "
  155.                     ErrorMessageBox( str, TITLE );          //  Display error and break
  156.                     break;
  157.                 }
  158.                                                                       //  Get H. spacing value
  159.                 val = GetDlgItemInt( hDlg, 102, &bJunk, FALSE );
  160.                 wsprintf( str, pd, val );                        //  Convert to string
  161.                 dwSize = lstrlen( str ) + 1;                 //  Note the double NULLs
  162.  
  163.                 LoadString( hInst, 202, resname, 64 );             //  "IconSpacing"
  164.                                                 //  Attempt registry write of new value
  165.                 lStatus = RegSetValueEx( hKey, resname, (DWORD)0, REG_SZ, 
  166.                                                  str, dwSize );
  167.                 if( lStatus != ERROR_SUCCESS )       //  Didn't work...display error
  168.                 {
  169.                     LoadString( hInst, 103, resname, 64 );    //  "Write IconSpacing"
  170.                     ErrorMessageBox( resname, TITLE );
  171.                 }
  172.  
  173.                 val = GetDlgItemInt( hDlg, 101, &bJunk, FALSE );   //  Go for V size
  174.                 wsprintf( str, pd, val );
  175.                 dwSize = lstrlen( str ) + 1;      
  176.  
  177.                 LoadString( hInst, 201, resname, 64 );        //  "IconVerticalSpacing"
  178.                 lStatus = RegSetValueEx( hKey, resname, (DWORD)0, REG_SZ, 
  179.                                                  str, dwSize );
  180.                 if( lStatus != ERROR_SUCCESS )
  181.                 {
  182.                     LoadString( hInst, 104, resname, 64 );      //  "Write IconVSpacing"
  183.                     ErrorMessageBox( resname, TITLE );
  184.                 }
  185.  
  186.                 RegCloseKey( hKey );
  187.  
  188.                 if( lStatus != ERROR_SUCCESS )
  189.                     break;
  190.                                     
  191.                 EndDialog( hDlg, TRUE );
  192.                 return( TRUE );
  193.             }
  194.             break;
  195.     }
  196.     return( FALSE );
  197. }
  198.  
  199. // ----------------------------------------------------------------------------
  200.  
  201. BOOL ChangeDesktopFont( HWND hDlg )
  202. {
  203.     CHOOSEFONT cf;
  204.     LOGFONT lf;
  205.     HKEY hKey;
  206.     HDC hDC;
  207.     BOOL bFlag;
  208.     LONG lStatus;
  209.     char szReg[ 256 ];
  210.     DWORD dwType, dwSize;
  211.     char szValue[ 32 ];
  212.     int len;
  213.  
  214.     LoadString( hInst, 200, szReg, 64 );             //  "Control Panel\\Desktop"
  215.     memset( (LPVOID)&lf, 0, sizeof(LOGFONT) );         //  Clear LOGFONT structure
  216.                                                                //  Attempt to open key above
  217.     lStatus = RegOpenKeyEx( HKEY_CURRENT_USER, szReg, (DWORD)0,
  218.                                     KEY_ALL_ACCESS, &hKey );
  219.     if( lStatus != ERROR_SUCCESS )                                 //  Didn't work...
  220.     {
  221.         LoadString( hInst, 102, szReg, 64 );
  222.         ErrorMessageBox( szReg, TITLE );
  223.         return( FALSE );
  224.     }
  225.  
  226.     bFlag = FALSE;
  227.     LoadString( hInst, 204, szValue, 32 );                //  "IconTitleFaceName"
  228.  
  229.     dwSize = 32;                                            //  Get font name from registry
  230.     lStatus = RegQueryValueEx( hKey, szValue, (LPDWORD)0, &dwType,
  231.                                         szValue, &dwSize );
  232.     if( lStatus == ERROR_SUCCESS )                      //  If that worked, continue
  233.     {
  234.         lstrcpy( lf.lfFaceName, szValue );
  235.         LoadString( hInst, 205, szValue, 32 )                     //  "IconTitleSize"
  236.         dwSize = 32;                                        //  Get font size from registry
  237.         lStatus = RegQueryValueEx( hKey, szValue, (LPDWORD)0, &dwType,
  238.                                             szValue, &dwSize );
  239.         if( lStatus == ERROR_SUCCESS )                        //  If step 2 worked...
  240.         {
  241.             lf.lfHeight = atol( szValue );
  242.             LoadString( hInst, 206, szValue, 32 );             //  "IconTitleStyle"
  243.             dwSize = 32;                                    //  Get style from registry
  244.             lStatus = RegQueryValueEx( hKey, szValue, (LPDWORD)0, &dwType,
  245.                                                 szValue, &dwSize );
  246.             if( lStatus == ERROR_SUCCESS )
  247.             {
  248.                 if( atol( szValue ) )           //  At this point, we have all we need
  249.                     lf.lfWeight = 800;
  250.                 else
  251.                     lf.lfWeight = 400;
  252.                                                                       //  Clear CHOOSEFONT struct
  253.                 memset( (LPVOID)&cf, 0, sizeof(CHOOSEFONT) );
  254.                 hDC = GetDC( hDlg );
  255.  
  256.                 cf.lStructSize = sizeof(CHOOSEFONT);
  257.                 cf.hwndOwner = hDlg;
  258.                 cf.hDC = hDC;
  259.                 cf.lpLogFont = &lf;
  260.                 cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
  261.  
  262.                 bFlag = ChooseFont( &cf );                        //  Display font data
  263.                 ReleaseDC( hDlg, hDC );
  264.                 if( bFlag )                                     //  If user pressed "OK"...
  265.                 {
  266.                     dwType = REG_SZ;               //  Type is always a plain string
  267.                     LoadString( hInst, szReg, 204, 32 );     //  "IconTitleFaceName"
  268.                     lstrcpy( szValue, lf.lfFaceName );
  269.                     szValue[ lstrlen( szValue ) ] = '\0';          //  Write font name
  270.                     RegSetValueEx( hKey, szReg, 0, dwType, szValue,     //  to registry
  271.                                         lstrlen( szValue ) + 1 );
  272.  
  273.                     LoadString( hInst, 205, szReg, 32 );           //  "IconTitleSize"
  274.                     wsprintf( szValue, "%d\0", lf.lfHeight );
  275.                     RegSetValueEx( hKey, szReg, 0, dwType, szValue,      //  Write size
  276.                                         lstrlen( szValue ) + 1 );
  277.  
  278.                     LoadString( hInst, 206, szReg, 32 );          //  "IconTitleStyle"
  279.                     wsprintf( szValue, "%d\0", ( lf.lfWeight > 400 ) );
  280.                     RegSetValueEx( hKey, szReg, 0, dwType, szValue,     //  Write style
  281.                                          lstrlen( szValue ) + 1 );
  282.                 }
  283.             }
  284.         }
  285.     }
  286.  
  287.     RegCloseKey( hKey );                    //  Close key and font stuff is done!
  288.     return( bFlag );
  289. }    
  290.  
  291. // ---- One Size Fits All "Oops" Message Box -----------------------------------
  292.  
  293. BOOL ErrorMessageBox( LPSTR lpszText, LPSTR lpszTitle )
  294. {
  295.  
  296.    LPSTR lpFormatMessageBuffer;
  297.    DWORD  dwFormatMessage;
  298.    DWORD  dwGetLastError;
  299.    HLOCAL hMessageBoxBuffer;
  300.    LPVOID lpMessageBoxBuffer;
  301.  
  302.    dwGetLastError = GetLastError();                        //  Get system error
  303.  
  304.    hMessageBoxBuffer  = LocalAlloc( LMEM_FIXED, 1024 );       //  Get some memory
  305.    lpMessageBoxBuffer = LocalLock( hMessageBoxBuffer );            //  for strings
  306.                                                         //  Get the system error string
  307.    dwFormatMessage = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
  308.                                     FORMAT_MESSAGE_FROM_SYSTEM, NULL, 
  309.                                               dwGetLastError, LANG_NEUTRAL,
  310.                                     (LPSTR)&lpFormatMessageBuffer, 0, NULL );
  311.    if( ! dwFormatMessage )
  312.         return( FALSE );
  313.                                                             //  Initialize display string
  314.    wsprintf( lpMessageBoxBuffer, "%s %s", lpszText, lpFormatMessageBuffer );
  315.  
  316.    MessageBox( NULL, lpMessageBoxBuffer, lpszTitle, 
  317.                     MB_ICONEXCLAMATION | MB_OK );
  318.  
  319.    if( dwFormatMessage )
  320.       LocalFree( (HLOCAL) lpFormatMessageBuffer );
  321.  
  322.    LocalFree( (HLOCAL) hMessageBoxBuffer );                 //  Clean up and exit
  323.    return( TRUE );
  324. }
  325.  
  326. // ----------------------------------------------------------------------------
  327.